<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\bootstrap\ActiveForm;
use kartik\editable\Editable;

$models = $model->room; // Table OrderRoom
//$arr = \yii\helpers\ArrayHelper::map($models, 'id', 'id', 'room_id');
// 计算舱房数量
$arr = [];
$is_merge = false;
foreach($models as $v){
	if($model->product->stock == 1) {

        $name = $v->productRoom->room->name . $v->productRoom->room->code .' '. $v->productRoom->people.'人间';

	}elseif($model->product->stock == 2){ // 海外航线
		$t = json_decode($model->product->rooms, true);
		$name = isset($t[$v->room_id]['name']) ? $t[$v->room_id]['name'] : '异常';
		$name .= ' ';
		$name .= isset($v->num) ? $v->num.'人间' : '';
	}
	$arr[$name][] = $v->id;
	
	//  是否有选择拼房
	if($model->is_merge == 2){
		if($v->merge_id){
			$is_merge = 1;
		}
	}
}
?>

<?=  yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>

<div class="col-md-offset-1 col-md-10 panel panel-body" style="margin-top:30px;">
    <div class="page-header" style="margin-top:1px;padding-bottom:2px;">
		<!-- a class="label label-danger fa fa-plus" data-toggle="modal" data-target=".room-modal" >&nbsp;<small>添加舱房</small></a -->
		<p>
			<small>
			舱房共计 <?= count($models) ?> 间 : &nbsp;
			<?php if($arr){ ?>
			<?php foreach($arr as $key=>$val){ ?>
				  <?= $key ?> <?= count($val)?> 间 |
			<?php }} ?>
			</small>
		</p>
	</div>

    <?php foreach($models as $mk=>$room){ ?>
    <div class="row" style="border-bottom: 1px solid #F1F1F1;padding:8px 0px;">
        <div class="col-md-4" >
		<?php if($model->is_merge == 2){ ?>
			<?php if($is_merge){ ?>
				<?php if($room->merge_id){ ?> 
				<a class="fa fa-handshake-o" style="color:red;" href="/order/order/merge-list?id=<?= $model->id?>&room_id=<?= $room->id ?>" data-toggle='modal' data-target='.customer-modal' title='拼房标识' ></a>
				<?php } ?>
			<?php }else{  ?>
				<a class="fa fa-handshake-o"  href="/order/order/merge-list?id=<?= $model->id?>&room_id=<?= $room->id ?>" data-toggle='modal' data-target='.customer-modal' title='拼房标识' ></a>
		<?php } } ?>
		<a data-toggle="collapse" href="#collapseExample<?= $room->id?>" aria-expanded="false" aria-controls="collapseExample<?= $room->id?>" >
			<?php if($model->product->stock == 1) { ?>

        	    <?= $room->productRoom->room->name ?>
        	    <?= $room->productRoom->room->code ?>
        	    <?= $room->productRoom->people ?>人间
			<?php }elseif($model->product->stock == 2){ // 海外航线

					$t = json_decode($model->product->rooms, true);
					echo isset($t[$room->room_id]['name']) ? $t[$room->room_id]['name'] : '异常';
					echo "  - ".$room->num . "人";
				}
			?>
		</a>

		<font style="margin-left: 30px;">后返 :
			<?php echo Editable::widget([
    		    'name' => 'OrderRoom[commiss_price]',
				'value' => $room->commiss_price,
				'displayValue' => Yii::$app->formatter->asCurrency($room->commiss_price),
    		    'asPopover' => true,
    		    'header' => '后返金额',
    		    'size'=>'md',
    		    'formOptions' => [
    		    	'action'=>'/order/room/commiss-ajax?OrderRoom[id]='.$room->id,
    		    	'method'=>'get',
    		    	],
    		    'options' => ['class'=>'form-control', 'placeholder'=> '后返金额'],
    		]);?>
		</font>
        </div>
        <div class="col-md-6">
            <?php $exit = yii\helpers\ArrayHelper::map($room->orderRoomCustomer, 'id', 'customer_id');?>
            <?php foreach($customers as $customer){ ?>
            <div style="float:left;width:100px;">
                <input type="hidden" class="room-id" value="<?= $room->id?>">
                <?php if(in_array($customer->id,$exit)){ ?>
                <input class="magic-checkbox room-magic" <?= in_array($customer->id, $exit) ? 'checked' : '' ?> type="checkbox" id="room<?= $mk.$customer->id?>" name="<?= $customer->id?>"  >
                	<label for="room<?= $mk.$customer->id ?>"  style="display:inline;" title="取消该用户">
                		<a data-toggle="modal" data-target=".customer-modal"  title="编辑该用户" href="/order/customer/update?id=<?= $customer->id?>" data="<?= $customer->id ?>" >
							<?php $name = $customer->first_name.$customer->last_name; ?>
							<?= empty($name) ? '--' : $name ?>
						</a>
					</label>
					<?php if($customer->complete == 1){ ?>
						<i class="fa  fa-check-square" title="信息完整"></i>
					<?php } ?>
                <?php }?>
            </div>
            <?php } ?>
        </div>
		<div class="col-md-2 text-right">
        	<a data-toggle="modal" data-target=".customer-modal"  href="/order/customer/create-new?order_id=<?= $model->id?>&order_room_id=<?= $room->id ?>" class=" fa fa-user-plus" title="添加客户"></a>
			&nbsp;&nbsp;&nbsp;

        	<a class="refund" title="删除舱房" style="cursor:pointer;" data-toggle="modal" data-target=".refund-modal" data-id=<?= $room->id ?>><i class="fa fa-remove"></i></a>
		</div>
		<!-- 舱房的 一些辅助信息 -->
		<div class="col-md-12 collapse" id="collapseExample<?= $room->id?>">
		    <div class="alert row">
		  		<div class="col-md-2">金额 : <?= $room->price ?></div>
		  		<div class="col-md-2">入住人数 : <?= $room->num ?>人</div>
		  		<div class="col-md-2">房号: <?= empty($room->code) ? "未设置" : $room->code ?></div>
		  		<div class="col-md-3">后返金额上限: <?= $room->maxCommiss?></div>
		    </div>
		</div>
    </div>
    <?php } ?>
</div>

<!-- 取消的舱房 -->
<?php if($model->refund ){ ?>
<div class="col-md-offset-1 col-md-10 panel panel-body" style="margin-top:10px;">
	<h4 class="page-header" style="margin-top:5px;"><small class="text-warning">取消舱房列表</small></h4>
	<table class="table" style="color: #999;">  
		<tr>
			<td>房型</td>
			<td>舱房金额</td>
			<td>扣除金额</td>
			<td>退款金额</td>
			<td>取消原因</td>
			<td>审核</td>
		</tr>

		<?php foreach($model->refund as $refund) { ?>
		<tr>
			<td class="col-md-3">
				<?= $refund->room ?>
			</td>
			<td><?= Yii::$app->formatter->asCurrency($refund->room_price) ?></td>
			<td><?= Yii::$app->formatter->asCurrency($refund->price) ?></td>
			<td><?= Yii::$app->formatter->asCurrency($refund->room_price-$refund->price) ?></td>
			<td><?= $refund->desc ?></td>
			<td class="col-md-1"><label class="label <?= ($refund->status==2) ? 'label-success' : 'label-danger' ?>"><?= \Yii::$app->params['refund'][$refund->status] ?></label></td>
		</tr>
		<?php } ?>
	</table>
</div>
<?php } ?>

<div class="modal fade customer-modal" id="customer-modal" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:1060px;">
      <div class="modal-content">
	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>


<div class="modal fade room-modal" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:660px;">

      <div class="modal-content">
	    	<?php $form = ActiveForm::begin([
                         'action'=>'/order/order/edit-room?id='.$model->id,
                         'layout' => 'horizontal',
                    ]); ?>
  	  	    <div class="modal-header">
  	  	    	<button type="button" class="close" data-dismiss="modal" aria-label="Close">
	  	        	<span aria-hidden="true">&times;</span>
	  	        </button>
  	  	    	<h4 class="modal-title"><small>添加舱房</small></h4>
  	  	    </div>
	  		<div class="modal-body">
	 			<!-- 嵌入内容 --> 
				<?php  echo $this->render('_change', ['model'=>$model, 'form'=>$form]); ?>
	  		</div>

  	  	    <div class="modal-footer">
        		<?= Html::submitButton('添加', ['class' => 'btn btn-primary']) ?>
	  		</div>
    		<?php ActiveForm::end(); ?>
	  </div>
  </div>
</div>


<div class="modal fade refund-modal" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" >
      <div class="modal-content">
  	  	  <div class="modal-header">
  	  	  	  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
	  	      	<span aria-hidden="true">&times;</span>
	  	      </button>
  	  	  	  <h4 class="modal-title">订单舱房取消</h4>
  	  	  </div>
      	  <div class="modal-body">
		  	  <textarea id="order-refund" class="form-control" placeholder="取消原因" maxlength="255" rows="4"></textarea>
			  <input type="hidden" id="refund-room-id" value="">
		  </div>
		  <div class="modal-footer">
		  	  <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
		  	  <button type="button" class="btn btn-primary" id="remove-room" >确定</button>
		  </div>
	  </div>
  </div>
</div>



<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
    /**  add Loading  **/
    $(".room-magic").click(function(){
        var customer_id   = $(this).attr('name'); // 获取客户编号
        var order_room_id = $(this).siblings('.room-id').val(); // 获取客户编号
        var checked = $(this).is(':checked');   // 是否选中

		if(checked == false){
			if(confirm('确认执行该操作吗?') == false){
				return false;
			}
		}
		var m = $(this);
		m.attr("disabled","disabled");
		// disable

        $.get("/order/room-customer/save", {customer_id:customer_id,order_room_id:order_room_id,checked:checked}, function(data){

			if(data == 'order-price-exit'){
				alert("该用户已分配附加费不能取消, 请先从附加费中移除该用户");	
				window.location.reload();
			}else if(data == 'order-assign-exit'){

				alert("该用户所在订单已经分团不能取消, 请先从分团中移除对应订单");	
				window.location.reload();
			}else if(data == 'delete'){

				alert("取消成功");	// 防止未删除对应关系 便阻止程序
			}else if(data == 'success'){

				alert("绑定成功");	// 防止未绑定完成对应关系 便阻止程序
			}

			// show 
			m.removeAttr("disabled");
        });
    });
	// 定位删除订单
    $(".refund").click(function(){
        var id = $(this).data('id'); // 房间 order_room 表 ID

		$("#refund-room-id").val(id);
		//$("#remove-room").text(id);
	});
	// 取消舱房
    $("#remove-room").click(function(){
        // var id = $(this).data('id'); // 房间 order_room 表 ID
		var id = $("#refund-room-id").val();
		var desc = $("#order-refund").val();

        $.get("/order/room/remove", {id: id, desc: desc}, function(data){
			//console.log(data);
			//alert(data);
			if(data == 'true'){

				 window.location.reload();
			}else if(data == 'error1'){

				alert("请先取消舱房填写的入住人");
			}else if(data == 'error2'){
				alert("请先取消拼住的舱房");
			}else{
			
			}
        });
    });

	// 清除模态框缓存
	$(".customer-modal").on("hidden.bs.modal", function() {
		$(this).removeData("bs.modal");
		//window.location.reload();
	});
</script>
<?php app\components\JsBlock::end() ?>
